#include "tools.hpp"

namespace tools
{
	int get_abs_timespec(long ms, struct timespec & ts)
	{
		struct timeval tv;
		::gettimeofday(&tv, 0);
		const long nsec = tv.tv_usec * 1000 + ms * NSEC_TO_MS;
		ts.tv_nsec = nsec % NSEC_TO_SEC;
		ts.tv_sec  = tv.tv_sec + nsec / NSEC_TO_SEC;
		
		return 0;
	}
	
	int log_err_msg(int err, const char * msg, const char * file, int line)
	{
		const char * ptr = ::strrchr(file, '/');
		ptr  = ((NULL == ptr)? file: (ptr + 1));
		
		const static size_t SIZE = 1 << 9;
		char theMsg[SIZE];
		::strerror_r(err, theMsg, SIZE);
		LOG_STREAM_ERROR << ptr << "(" << line << ")  " << msg << ", error: " << 
						err << " (" << theMsg << ")" <<
		LOG_EOM;

		return 0;
	}
};

